package com.gin.no_gay_alliance.manager.provider;

import androidx.annotation.Nullable;

import com.fasterxml.jackson.core.type.TypeReference;
import com.gin.common.utils.FileIoUtils;
import com.gin.nga.callback.NgaCallback;
import com.gin.nga.resource.ResourceProvider;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.callback.Callback;
import com.gin.no_gay_alliance.viewmodel.emote.ThirdPartyEmoteCategory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;

/**
 * 第三方表情包链接地址
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/19 13:51
 **/
public class ThirdPartyEmoteUrlProvider extends ResourceProvider<List<String>> {
    /**
     * 表情包地址文件
     */
    public static final String URL = "https://gitee.com/AgMonk/no-gay-alliance-for-android/raw/master/assets/emotes/url.json";
//    public static final String URL = "https://agmonk.gitee.io/no-gay-alliance-for-android/assets/emotes/url.json";
    /**
     * 文件名
     */
    public static final String FILENAME = "third_party_emote_url.json";

    public ThirdPartyEmoteUrlProvider() {
        super(URL, MyApplication.app.okHttpClient, ResourceManager.getRecourceCacheFile(FILENAME), TimeUnit.HOURS.toMillis(6));
    }


    @Override
    protected List<String> readCache(File cacheFile) throws IOException {
        return FileIoUtils.readObj(cacheFile, new TypeReference<List<String>>() {
        });
    }

    /**
     * 请求第三方表情分类
     */
    public static void requestThirdPartyEmoteCategories(@Nullable Callback<List<ThirdPartyEmoteCategory>> callback) {
        // 提交给线程池处理
        MyApplication.app.threadPoolExecutor.execute(() -> {
            try {
                List<ThirdPartyEmoteCategory> data = requestThirdPartyEmoteCategories(false);
                if (callback != null) {
                    callback.onSuccess(data);
                }
            } catch (IOException e) {
                if (callback != null) {
                    callback.onFailure(null, e);
                }
            }
        });
    }

    /**
     * 请求第三方表情分类
     *
     * @return 第三方表情分类
     */
    public static List<ThirdPartyEmoteCategory> requestThirdPartyEmoteCategories(boolean ignoreCache) throws IOException {
        // 分类地址
        List<String> urls = new ThirdPartyEmoteUrlProvider().sync(ignoreCache);
        CountDownLatch latch = new CountDownLatch(urls.size());
        // 创建响应
        LinkedHashMap<Integer, ThirdPartyEmoteCategory> res = new LinkedHashMap<>(urls.size());
        for (int i = 0; i < urls.size(); i++) {
            String url = urls.get(i);
            int finalI = i;
            new ThirdPartyEmoteCategoryProvider(url).async(ignoreCache, new NgaCallback<ThirdPartyEmoteCategory>() {
                @Override
                public void onSuccess(ThirdPartyEmoteCategory data) {
                    res.put(finalI, data);
                    latch.countDown();
                }

                @Override
                public void onFailure(Call call, IOException e) {
                    latch.countDown();
                }
            });
        }
        try {
            latch.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new ArrayList<>(res.values());
    }
}
